<?php
namespace app\proxy\controller;
use think\Db;
class Chefei extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}

	/**
	 * 技师等级列表
	 */
	public function index()
	{
	    
        $proxy = session('proxy');

        $proxy_id = $proxy['proxy_id'];
        
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{
				$map = [
					'proxy_id' => $proxy_id,
					'is_delete_time'=>0
				];
				

            
				$count = Db::name('chefei')->where($map)->count();
				
				$list_data = Db::name('chefei')->where($map)->order('id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
				
				if(!empty($list_data))
				{
					//遍历处理
					foreach($list_data as $key => $value)
					{
						//编辑url
						$list_data[$key]['save_url'] = url('chefei/saveinfo', ['id' => $value['id']]);
					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else
				{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
			return $this->fetch();
		}
	}

	/**
	 * 技师等级添加/修改 页面
	 */
	public function saveinfo()
	{
	    
        $proxy = session('proxy');

        $proxy_id = $proxy['proxy_id'];
        
		// 参数
		$params = input();
		$info = [];
			//查询
			$info = Db::name('chefei')->where(['proxy_id' => $proxy_id])->find();
		$this->assign('info',$info);
		return $this->fetch();
	}

	/**
	 * 技师等级添加/修改
	 */
	public function save()
	{
	    
        $proxy = session('proxy');

        $proxy_id = $proxy['proxy_id'];
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			
			
			// 添加/编辑
			if(empty($params['id']))
			{
			    
        		$params['upd_time'] = time();
        		$params['proxy_id'] = $proxy_id;
        		$techn_level_id = Db::name('chefei')->insertGetId($params);
			}else
			{
				$params['upd_time'] = time();
				if(Db::name('chefei')->where(['proxy_id' => $proxy_id])->update($params))
				{
					$techn_level_id = $params['id'];
				}
			}
			//是否成功
			if(isset($techn_level_id) && $techn_level_id > 0)
			{
				$result['code'] = 0;
				$result['msg'] = "操作成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "操作失败";
			}
		}
		echo json_encode($result);
	}


	
}